﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using StockCrawlerLib.DAL;
using DevExpress.Web;
using DevExpress.Data;
using CrawlerController;
using StockCrawlerLib;
using System.Data;

namespace StockCrawler
{

    public partial class StockFilter2 : System.Web.UI.Page
    {
        private StockRepository _stockRepo = new StockRepository();
        private HistoryRepository _hisRepo = new HistoryRepository();
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                //rbnAll.Checked = true;
                //lblNumDaysBefore.Text = "Số ngày giảm liên tục";
                //cboNumDaysBefore.SelectedIndex = 2;
                //lblNumDaysAfter.Text = "Sau khi tăng liên tục";
                //cboNumDaysAfter.SelectedIndex = 0;
                //cboChangedRange.SelectedIndex = 0;
                //cboVolumnRange.SelectedIndex = 0;
                InitMasterGrid();
            }
        }

        private void InitMasterGrid()
        {
            //cboNumDaysBefore.Enabled = !rbnAll.Checked;
            //cboNumDaysAfter.Enabled = !rbnAll.Checked;
            //cboChangedRange.Enabled = !rbnAll.Checked;
            //cboVolumnRange.Enabled = !rbnAll.Checked;
            //chkIncludeNoneChangeBefore.Enabled = !rbnAll.Checked;
            //chkIncludeNoneChangeAfter.Enabled = !rbnAll.Checked;
            gridViewHistory.DataSource = null;
            gridViewHistory.DataBind();

            gridViewStock.DataBind();
        }

        protected void gridViewHistory_CustomCallback(object sender, ASPxGridViewCustomCallbackEventArgs e)
        {
            string[] args = e.Parameters.Split('|');
            if (args == null || args.Length == 0)
                return;
            gridViewHistory.DataSource = null;
            if (gridViewStock.VisibleRowCount > 0)
            {
                if (args[0].Equals("INDEX"))
                {
                    int id_stock = (int)gridViewStock.GetRowValues(Convert.ToInt32(args[1]), new string[] { "id_stock" });
                    Session["id_stock"] = id_stock;

                    var dt = _hisRepo.Select2(id_stock);
                    gridViewHistory.DataSource = dt;
                    gridViewHistory.DataBind();
                }
                else if (args[0].Equals("SORT"))
                {
                    int id_stock = (int)Session["id_stock"];

                    var dt = _hisRepo.Select2(id_stock);
                    gridViewHistory.DataSource = dt;
                    gridViewHistory.DataBind();
                    if (args[1] == "ngay_giao_dich")
                    {
                        ColumnSortOrder order = args[2] == "ASC" ? ColumnSortOrder.Ascending : ColumnSortOrder.Descending;
                        gridViewHistory.SortBy(gridViewHistory.Columns[args[1]], order);
                    }
                }
                else if (args[0].Equals("PAGING"))
                {
                    int id_stock = (int)gridViewStock.GetRowValues(gridViewStock.VisibleStartIndex, new string[] { "id_stock" });
                    //int id_stock = (int)gridViewStock.GetRowValues(Convert.ToInt32(args[1]), new string[] { "id_stock" });
                    Session["id_stock"] = id_stock;

                    var dt = _hisRepo.Select2(id_stock);
                    gridViewHistory.DataSource = dt;
                    gridViewHistory.DataBind();
                }
            }
        }

        protected void gridViewStock_DataBound(object sender, EventArgs e)
        {
            Page.ClientScript.RegisterStartupScript(this.GetType(), "myScript", "getTest();", true);
        }

        protected void gridViewStock_HtmlRowPrepared(object sender, ASPxGridViewTableRowEventArgs e)
        {
            try
            {
                if (e.RowType != GridViewRowType.Data) return;

                bool loaicp = Convert.ToBoolean(e.GetValue("loaicp"));
                if (!loaicp)
                {
                    e.Row.BackColor = System.Drawing.Color.LightGray;
                }
                else
                {
                    float eps = Convert.ToSingle(e.GetValue("eps"));
                    if (eps > 3000)
                    {
                        e.Row.ForeColor = System.Drawing.Color.Red;
                    }
                }

            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        protected void gridViewHistory_HtmlDataCellPrepared(object sender, ASPxGridViewTableDataCellEventArgs e)
        {
            try
            {
                if (e.DataColumn.FieldName == "thay_doi")
                {
                    float gia_dong_cua = (float)gridViewHistory.GetRowValues(e.VisibleIndex, new string[] { "gia_dong_cua" });
                    float thay_doi = (float)gridViewHistory.GetRowValues(e.VisibleIndex, new string[] { "thay_doi" });
                    float gia_tran = (float)gridViewHistory.GetRowValues(e.VisibleIndex, new string[] { "gia_tran" });
                    float gia_san = (float)gridViewHistory.GetRowValues(e.VisibleIndex, new string[] { "gia_san" });
                    if (thay_doi < 0)
                    {
                        if (gia_dong_cua == gia_san)
                            e.Cell.ForeColor = System.Drawing.Color.FromArgb(0x66, 0xCC, 0xFF);
                        else
                            e.Cell.ForeColor = System.Drawing.Color.FromArgb(0xFF, 0x00, 0x00);
                    }
                    else if (thay_doi > 0)
                        if (gia_dong_cua == gia_tran)
                            e.Cell.ForeColor = System.Drawing.Color.FromArgb(0xFF, 0x00, 0xFF);
                        else
                            e.Cell.ForeColor = System.Drawing.Color.FromArgb(0x00, 0xFF, 0x00);
                    else
                    {
                        e.Cell.ForeColor = System.Drawing.Color.FromArgb(0xF7, 0xFF, 0x31);
                    }
                }
                else if (e.DataColumn.FieldName == "gia_dong_cua" || e.DataColumn.FieldName == "gia_mo_cua"
                    || e.DataColumn.FieldName == "gia_cao_nhat" || e.DataColumn.FieldName == "gia_thap_nhat")
                {
                    float gia_tham_chieu = (float)gridViewHistory.GetRowValues(e.VisibleIndex, new string[] { "gia_tham_chieu" });
                    float gia_tran = (float)gridViewHistory.GetRowValues(e.VisibleIndex, new string[] { "gia_tran" });
                    float gia_san = (float)gridViewHistory.GetRowValues(e.VisibleIndex, new string[] { "gia_san" });

                    float value = (float)e.CellValue;
                    if (value == gia_san)
                        e.Cell.ForeColor = System.Drawing.Color.FromArgb(0x66, 0xCC, 0xFF);
                    else if (value == gia_tran)
                        e.Cell.ForeColor = System.Drawing.Color.FromArgb(0xFF, 0x00, 0xFF);
                    else if (value < gia_tham_chieu)
                        e.Cell.ForeColor = System.Drawing.Color.FromArgb(0xFF, 0x00, 0x00);
                    else if (value > gia_tham_chieu)
                        e.Cell.ForeColor = System.Drawing.Color.FromArgb(0x00, 0xFF, 0x00);
                    else
                        e.Cell.ForeColor = System.Drawing.Color.FromArgb(0xF7, 0xFF, 0x31);
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }

        }

        protected void ASPxCallback1_Callback(object sender, CallbackEventArgs e)
        {
            try
            {
                string[] args = e.Parameter.Split('|');
                if (args == null || args.Length == 0)
                    return;
                if (args[0].Equals("INDEX"))
                {
                    if (gridViewStock.VisibleRowCount > 0)
                    {
                        CafefCrawler cafef = new CafefCrawler();
                        string code = gridViewStock.GetRowValues(Convert.ToInt32(args[1]), new string[] { "code" }).ToString();
                        e.Result = cafef.GetCurrentInfo(code);

                        //FptsCrawler cafef = new FptsCrawler();
                        //string code = gridViewStock.GetRowValues(Convert.ToInt32(args[1]), new string[] { "code" }).ToString();
                        //stock obj = _stockRepo.Select(code);
                        //e.Result = cafef.GetCurrentInfo(obj.id_fpt.ToString());
                    }
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }

        }


        protected void chkIsRemark_CheckedChanged(object sender, EventArgs e)
        {
            ASPxCheckBox chk = (ASPxCheckBox)sender;
            int id_stock = (int)gridViewStock.GetRowValues(gridViewStock.FocusedRowIndex, new string[] { "id_stock" });
            if (id_stock > 0)
            {
                Session["id_stock"] = id_stock;
                _stockRepo.UpdateIsRemark(id_stock, chk.Checked);
                InitMasterGrid();
            }
        }

        protected void LinqServerModeDataSourceStock_Selecting(object sender, DevExpress.Data.Linq.LinqServerModeDataSourceSelectEventArgs e)
        {
            e.KeyExpression = "id_stock";
            e.QueryableSource = _stockRepo.GetDefaultStockList(0);
        }
    }
}
